import Vue from 'vue'
import Router from 'vue-router'
import { constantRouterMap } from './router.config'

// hack router push callback
const originalPush = Router.prototype.push
Router.prototype.push = function push(location: any, onResolve: any, onReject: any) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch((err: any) => err)
}

Vue.use(Router)

const createRouter = () =>
  new Router({
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRouterMap
  })

const router = createRouter()

router.beforeEach((to: any, from: any, next: any) => {
  document.title = to.meta.title
  next()
})

router.afterEach((to: any, from: any) => {})

export default router
